<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
$action = $_GET["action"];
$page_xml = $_GET['page_xml'];
$component_id = $_GET['component'];
$selected_path = $_GET['selected_path'];
$selected_file = $_GET['selected_file'];

if(!isset($semf_root) )
{
	$semf_root = '../';
	// Define that we are returning XML content & not to cache:
	header('Content-Type: text/xml');
	header('Cache-control: no-cache');
}

include_once("util/dom.php");
include_once("util/dirutils.php");

if( $action == "open")
{
	echo "<msg>";
	echo "successfull";
	echo "</msg>";
}
elseif( $action == "save")
{
	$doc = new DOMDocument();
	
	
	if(file_exists($page_xml)) {
		$doc->load($page_xml);
		$doc->validate();
		$component_xml = $doc->getElementById($component_id);
		
		if(!is_dir($selected_path))
		{
			$selected_path = dirname($selected_path);
		}
		//always get filename from the selectedFile
		$file_name = $selected_path."/".$selected_file;
		
		echo "saving to ".$file_name;
		
		// Create an instance of the DomImplementation class
		$impl = new DomImplementation;
		// Create an instance of the DomDocumentType class by
		// calling the DomImplementation::createDocumentType() method
		// (arguments: doctype_name, doctype_public, doctype_system)
		$relative_dir = find_relative_dir($file_name, 
							$semf_root."page_designer/"."component.dtd");
		$dtd = $impl->createDocumentType("component", "", $relative_dir);
		// Create an instance of the DomDocument class by
		// calling the DomImplementation::createDocument() method
		// (arguments: NS_URL, NS_PREFIX, DomDocumentType_instance)
		$new_doc = $impl->createDocument("", "", $dtd);
		// Set other properties
		$new_doc->encoding = "UTF-8";
		$new_doc->standalone = "no";
	
		appendNode($new_doc, $component_xml, $new_doc);
		
		$new_doc->save($file_name);
		
		echo "<msg>";
		echo "successfull";
		echo "</msg>";
	}
	else
	{
		echo "<msg>";
		echo $page_xml." doesnt exists";
		echo "</msg>";
	}
}


?>